package common.net
{
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.system.LoaderContext;
    import flash.utils.ByteArray;

    import kernel.debug.Debug;

    import org.granite.net.DataLoader;
    import org.granite.net.DataLoaderType;



    public class CustomImageLoader extends DataLoader
    {
        private var _customData:ByteArray;

//        private var _imageLoader:ImageLoader;
//        private var _dataLength:int;

        public function CustomImageLoader():void
        {

            super (DataLoaderType.BINARY);
        }

        public function get customData():ByteArray
        {
            return _customData;
        }

        public override function close():void
        {
//            if (_imageLoader != null)
//            {
//                _imageLoader.close ();
//            }
            super.close ();
        }

        public override function destroy(gc:Boolean = false):void
        {
            _customData = null;
//            if (_imageLoader != null)
//                _imageLoader.destroy (gc);
            super.destroy (gc);
        }

        protected override function _destroy():void
        {
//            if (_imageLoader != null)
//            {
//                _imageLoader.removeEventListener (IOErrorEvent.IO_ERROR , _imageLoadErrorHandler);
//                _imageLoader.removeEventListener (Event.COMPLETE , _imageLoadCompleteHandler);
//                _imageLoader = null;
//            }
            super._destroy ();
        }



        protected function _loadSuccess():void
        {
        }

        protected function _loadError():void
        {
        }

        //handlers
        protected override function _loadCompleteHandler(e:Event):void
        {
            var temp:ByteArray = _loader.data;
            var lc:LoaderContext = _context;
            //stop();
            _context = lc;
            _customData = new ByteArray ();
            if (temp.readUnsignedInt () == 0x89504e47)
            { //png
//                _dataLength = 0;
            }
            else
            {
//                _dataLength = _getCustomBytesLength (temp);
//                _customData.writeBytes (temp , 0 , _dataLength);
//                _customData.position = 0;
            }
            _loadSuccess ();
//            _imageLoader = new ImageLoader ();
//            _imageLoader.addEventListener (IOErrorEvent.IO_ERROR , _imageLoadErrorHandler , false , 0 , true);
//            _imageLoader.addEventListener (Event.COMPLETE , _imageLoadCompleteHandler , false , 0 , true);
//            var bytes:ByteArray = new ByteArray ();
//            bytes.writeBytes (temp , _dataLength);
//            _imageLoader.loadBytes (bytes , _context);
        }

        protected override function _loadIOErrorHandler(e:Event):void
        {
            _loadError ();
            Debug.error ("CustomImageLoader IOError:" , e);
            super._loadIOErrorHandler (e);
        }

        protected function _imageLoadCompleteHandler(e:Event):void
        {
            _loadSuccess ();
            dispatchEvent (e);
        }

        protected function _imageLoadErrorHandler(e:IOErrorEvent):void
        {
            _loadError ();
            Debug.error ("CustomImageLoader LoadError:" , e);
            dispatchEvent (e);
        }
    }
}
